/**
 * 
 */
package com.shine.icis.blo.imp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IChkLogService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.dao.IChkLogDAO;
import com.shine.icis.dao.param.ETLRunLogQueryParameters;
import com.shine.icis.dto.DictOption;
import com.shine.icis.hbean.ChkRunLog;

/**
 * @author zsy
 *
 */
public class ChkLogService implements IChkLogService {
	
	private IChkLogDAO chkLogDAO;

	public void setChkLogDAO(IChkLogDAO chkLogDAO) {
		this.chkLogDAO = chkLogDAO;
	}

	@Override
	public ListPage<ChkRunLog> queryChkRunLogPage(ETLRunLogQueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return chkLogDAO.findChkRunLogPage(qp);
	}
	/**
	 * 运行日志导出Excel
	 * @author zuohp 2012-09-18
	 * */
	@Override
	public String csExpChkRunLog(ETLRunLogQueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("股份核对运行日志");
		cfg.addField("序号", "logID", String.class, 6);
		cfg.addField("级别", "prepLevelText", String.class, 10);
		cfg.addField("操作日期", "formatOperDate", String.class, 10);
		cfg.addField("操作时间", "formatOperTime", String.class, 8);
		cfg.addField("类别", "etlProcText", String.class, 20);
		cfg.addField("状态", "runStatusText", String.class, 10);
		cfg.addField("信息情况", "resultMsg", String.class, 256);

		Export<ChkRunLog> exp = ExportFactory.createInstance(expType, cfg);
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<ChkRunLog> page = this.chkLogDAO.findChkRunLogPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.chkLogDAO.findChkRunLogPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
}
